package coba.mailing.mail;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import coba.mailing.body.Body12;
import coba.mailing.function.SumAtractivos;
import coba.mailing.jdbc.ConnectionFactory;
import coba.mailing.model.Atractivo;
import coba.mailing.model.AtractivoSum;
import coba.mailing.model.Solicitud;
import coba.mailing.procedure.AddAllCC;
import coba.mailing.procedure.AddAllTo;
import coba.mailing.properties.MainProperties;
import coba.mailing.query.FindAtractivoByItinerarioIdQuery;
import coba.mailing.query.FindSolicitudWhereFechaItinerarioBetween;

public class Mail12 {

	public static void sendAll() throws ClassNotFoundException, IOException, SQLException, EmailException {
		Connection con = ConnectionFactory.getConnection();

		Calendar today0Hour = GregorianCalendar.getInstance();

		today0Hour.set(Calendar.HOUR_OF_DAY, 0);
		today0Hour.set(Calendar.MINUTE, 0);
		today0Hour.set(Calendar.SECOND, 0);
		today0Hour.set(Calendar.MILLISECOND, 0);

		today0Hour.add(Calendar.DATE, 2);
		Date pasadoPrimeraHora = today0Hour.getTime();
		
		today0Hour.set(Calendar.HOUR_OF_DAY, 23);
		today0Hour.set(Calendar.MINUTE, 59);
		today0Hour.set(Calendar.SECOND, 59);
		today0Hour.set(Calendar.MILLISECOND, 999);
		Date pasadoUltimaHora = today0Hour.getTime();

		List<Solicitud> listSolicitud = FindSolicitudWhereFechaItinerarioBetween.find(
				con, pasadoPrimeraHora, pasadoUltimaHora);

		for (Solicitud solicitud : listSolicitud) {
			try {
				List<Atractivo> listAtractivo = FindAtractivoByItinerarioIdQuery.find(con, solicitud.getItinerarioId());
				List<AtractivoSum> listAtractivoSum = SumAtractivos.sumAtractivos(listAtractivo);

				if (listAtractivoSum.size() == 0)
					continue;

				send(solicitud, listAtractivoSum);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}

		con.close();
	}

	public static void send(Solicitud solicitud, List<AtractivoSum> listAtractivoSum) throws EmailException, IOException {
		MainProperties mainProperties = MainProperties.getInstance();

		HtmlEmail email = new HtmlEmail();

		email.setCharset("utf-8");
		email.setHostName(mainProperties.getProperty("smtpServer"));
		email.setSmtpPort(Integer.valueOf(mainProperties.getProperty("smtpPort")));

		AddAllTo.addAllTo(email, solicitud.getContactoEmail());
		AddAllCC.addAllCC(email, mainProperties.getProperty("mail12Ccs"));

		email.setFrom(mainProperties.getProperty("fromMail"), mainProperties.getProperty("fromName"));

		email.setSubject("ConociendoBA - VISITA " +
				new SimpleDateFormat("dd/MM/yyyy").format(solicitud.getFechaItinerario()) +
				" IMPORTANTE LEER!");

		String cid = email.embed(ClassLoader.getSystemClassLoader().getResource("coba-mail-footer.jpg"), "Conociendo footer");

		email.setHtmlMsg(Body12.get(cid, solicitud, listAtractivoSum));

		email.send();
	}

}
